//
//  TencentManager.m
//  TecentDemo
//
//  Created by leo on 9/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "TencentManager.h"
#import "NSURL+QAdditions.h"
#import "QOauthKey.h"
#import "QWeiboRequest.h"
#import "AdapterHelp.h"
#import "JsonStatusParser.h"

#define AppKey			@"appKey"
#define AppSecret		@"appSecret"
#define AppTokenKey		@"tokenKey"
#define AppTokenSecret	@"tokenSecret"
static TencentManager *tencentManager=nil;

@implementation TencentManager




#pragma ManagerDelegate

- (void)parseTokenKeyWithResponse:(NSString *)aResponse
{
	
	NSDictionary *params = [NSURL parseURLQueryString:aResponse];
	self.tokenKey = [params objectForKey:@"oauth_token"];
	self.tokenSecret = [params objectForKey:@"oauth_token_secret"];
	//self.userid = [params objectForKey:@"userid"];
}

- (void)saveDefaultKey {
	
	[[NSUserDefaults standardUserDefaults] setValue:self.appKey forKey:AppKey];
	[[NSUserDefaults standardUserDefaults] setValue:self.appSecret forKey:AppSecret];
	[[NSUserDefaults standardUserDefaults] setValue:self.tokenKey forKey:AppTokenKey];
	[[NSUserDefaults standardUserDefaults] setValue:self.tokenSecret forKey:AppTokenSecret];
	[[NSUserDefaults standardUserDefaults] synchronize];
}


- (void)loadDefaultKey
{
    self.appKey = [[NSUserDefaults standardUserDefaults] valueForKey:AppKey];
	self.appSecret = [[NSUserDefaults standardUserDefaults] valueForKey:AppSecret];
	self.tokenKey = [[NSUserDefaults standardUserDefaults] valueForKey:AppTokenKey];
	self.tokenSecret = [[NSUserDefaults standardUserDefaults] valueForKey:AppTokenSecret];
    
}

#pragma init

-(id)initWithDefault
{
    self = [super init];
    
    if (self) {
        self.VERIFY_URL = @"https://open.t.qq.com/cgi-bin/authorize?oauth_token=";
        self.appKey = @"eb3e78f26d6444cf9e34c82fce5546d4";
        self.appSecret = @"bb1b5b957226b61268dce29c2846f3a6";
        self.acces_token_url = @"https://open.t.qq.com/cgi-bin/access_token";
        self.request_token_url = @"https://open.t.qq.com/cgi-bin/request_token";
        
    }
    return self; 
    
}

+ (id)instance
{
    
    if(nil==tencentManager)
    {
        tencentManager =[[TencentManager alloc]initWithDefault];
    }
    
    return tencentManager;
}

//Get MSG public 
- (NSArray *)getHomeMsgWithPageFlage:(PageFlag)aPageFlag 
                             nReqNum:(NSInteger)aReqNum{
    
    NSArray *result =[ self getHomeMsgWithResultType:RESULTTYPE_JSON pageFlage:aPageFlag nReqNum:aReqNum];
    return result;
    
}


//private

- (NSArray *)getHomeMsgWithResultType:(ResultType)aResultType 
                            pageFlage:(PageFlag)aPageFlag 
                              nReqNum:(NSInteger)aReqNum {
	
	NSString *url = @"http://open.t.qq.com/api/statuses/home_timeline";
	
	QOauthKey *oauthKey = [self getOauthKey];
	
	NSString *format = nil;
	if (aResultType == RESULTTYPE_XML) {
		format = @"xml";
	} else if (aResultType == RESULTTYPE_JSON) {
		format = @"json";
	} else {
		format = @"json";
	}
	
	NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
	[parameters setObject:format forKey:@"format"];
	[parameters setObject:[NSString stringWithFormat:@"%d", aPageFlag] forKey:@"pageflag"];
	[parameters setObject:[NSString stringWithFormat:@"%d", aReqNum] forKey:@"reqnum"];
    
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSString *retString = [request syncRequestWithUrl:url httpMethod:@"GET" oauthKey:oauthKey parameters:parameters files:nil];
    NSArray * result = [JsonStatusParser parseToTencentStatuses:retString];
    
    
	[request release];
    [result autorelease];
	
	return result;
}

- (NSURLConnection *)getHomeMsgWithAsync:(ResultType)aResultType 
                               pageFlage:(PageFlag)aPageFlag 
                                 nReqNum:(NSInteger)aReqNum 
                                delegate:(id)aDelegate {
	
	NSString *url = @"http://open.t.qq.com/api/statuses/home_timeline";
	
    QOauthKey *oauthKey = [self getOauthKey];
	
	NSString *format = nil;
	if (aResultType == RESULTTYPE_XML) {
		format = @"xml";
	} else if (aResultType == RESULTTYPE_JSON) {
		format = @"json";
	} else {
		format = @"json";
	}
	
	NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
	[parameters setObject:format forKey:@"format"];
	[parameters setObject:[NSString stringWithFormat:@"%d", aPageFlag] forKey:@"pageflag"];
	[parameters setObject:[NSString stringWithFormat:@"%d", aReqNum] forKey:@"reqnum"];
	
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSURLConnection *connection = [request asyncRequestWithUrl:url httpMethod:@"GET" oauthKey:oauthKey parameters:parameters files:nil delegate:aDelegate];
	
	[request release];
	[oauthKey release];
	return connection;
}



- (NSString *)publishMsgWithConsumerKey:(NSString *)aConsumerKey 
						 consumerSecret:(NSString *)aConsumerSecret 
						 accessTokenKey:(NSString *)aAccessTokenKey 
					  accessTokenSecret:(NSString *)aAccessTokenSecret 
								content:(NSString *)aContent 
							  imageFile:(NSString *)aImageFile 
							 resultType:(ResultType)aResultType {
	
	NSMutableDictionary *files = [NSMutableDictionary dictionary];
	NSString *url;
	
	if (aImageFile) {
		url = @"http://open.t.qq.com/api/t/add_pic";
		[files setObject:aImageFile forKey:@"pic"];
	} else {
		url = @"http://open.t.qq.com/api/t/add";
	}
	
	QOauthKey *oauthKey = [[QOauthKey alloc] init];
	oauthKey.consumerKey = aConsumerKey;
	oauthKey.consumerSecret = aConsumerSecret;
	oauthKey.tokenKey = aAccessTokenKey;
	oauthKey.tokenSecret= aAccessTokenSecret;
	
	NSString *format = nil;
	if (aResultType == RESULTTYPE_XML) {
		format = @"xml";
	} else if (aResultType == RESULTTYPE_JSON) {
		format = @"json";
	} else {
		format = @"json";
	}
	
	NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
	[parameters setObject:aContent forKey:@"content"];
	[parameters setObject:@"127.0.0.1" forKey:@"clientip"];
	
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSString *retString = [request syncRequestWithUrl:url httpMethod:@"POST" oauthKey:oauthKey parameters:parameters files:files];
	
	[request release];
	[oauthKey release];
	return retString;
}
-(NSString *)getUserTimelineWihtResultType:(ResultType)aResultType 
                                 pageFlage:(PageFlag)aPageFlag
                                  PageTime:(long)pagetime
                                   nReqNum:(NSInteger)aReqNum
                                    Lastid:(long)lastid
                                      Name:(NSString *)name{
    return @"sdafasdfas";
}



@end
